package org.moore.controller;

import org.moore.view.GameFrame;
import org.moore.view.GamePanel;
import org.moore.view.WelcomeJFrame;

import javax.swing.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

// 游戏窗口监听类
public class GameWindowListener implements WindowListener {
    private final GameFrame gameFrame;
    private final WelcomeJFrame welcomeJFrame;
    private final GamePanel gamePanel;

    public GameWindowListener(GameFrame gameFrame, WelcomeJFrame welcomeJFrame, GamePanel gamePanel) {
        this.gameFrame = gameFrame;
        this.welcomeJFrame = welcomeJFrame;
        this.gamePanel = gamePanel;
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {
        String[] options = {"退出登录", "退出游戏", "返回主界面"};
        int response = JOptionPane.showOptionDialog(null, "请选择选项：", "游戏选项",
                JOptionPane.DEFAULT_OPTION, JOptionPane.PLAIN_MESSAGE, null, options, options[0]);
        if (response == 0){
            welcomeJFrame.setVisible(true);
            gameFrame.setVisible(false);
        } else if (response == 1){
            System.exit(0);
        } else if (response == 2){
            GamePanel.getCardLayout().show(gamePanel,"home");
        }
    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
}
